package com.wxp.demo08.fragment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.wxp.demo08.R;
import com.wxp.demo08.util.LogUtils;

/**
 * 动态注册碎片
 */
public class DynamicFragment extends Fragment {

    private final String TAG = DynamicFragment.class.getSimpleName();

    /**
     * 创建实例
     */
    public static DynamicFragment newInstance(int position, int image_id, String desc) {
        DynamicFragment fragment = new DynamicFragment();
        // 把参数打包，传入fragment中
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("image_id", image_id);
        args.putString("desc", desc);
        //设置碎片参数
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 从包裹取出位置序号
     */
    private int getPosition() {
        return getArguments().getInt("position", 0);
    }

    /**
     * 把碎片贴到页面上
     */
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        LogUtils.debug(TAG + " 把碎片贴到页面上：Fragment onAttach position=" + this.getPosition());
    }

    /**
     * 页面创建
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.debug(TAG + " 页面创建：Fragment onCreate position=" + this.getPosition());
    }

    /**
     * 创建视图方法
     */
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState
    ) {
        // 根据布局文件fragment_dynamic.xml生成视图对象
        View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
        Bundle arguments = this.getArguments();
        if (arguments != null) {
            ImageView iv_pic = view.findViewById(R.id.iv_pic);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            iv_pic.setImageResource(arguments.getInt("image_id", R.drawable.huawei));
            tv_desc.setText(arguments.getString("desc"));
        }
        LogUtils.debug(TAG + " 创建视图：Fragment onCreateView position=" + this.getPosition());
        return view;
    }

    /**
     * 在活动页面创建之后
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.debug(TAG + " 在活动页面创建之后：Fragment onActivityCreated position=" + this.getPosition());
    }

    /**
     * 页面启动
     */
    @Override
    public void onStart() {
        super.onStart();
        LogUtils.debug(TAG + " 页面启动：Fragment onStart position=" + this.getPosition());
    }

    /**
     * 页面恢复
     */
    @Override
    public void onResume() {
        super.onResume();
        LogUtils.debug(TAG + " 页面恢复：Fragment onResume position=" + this.getPosition());
    }

    /**
     * 页面暂停
     */
    @Override
    public void onPause() {
        super.onPause();
        LogUtils.debug(TAG + " 页面暂停：Fragment onPause position=" + this.getPosition());
    }

    /**
     * 页面停止
     */
    @Override
    public void onStop() {
        super.onStop();
        LogUtils.debug(TAG + " 页面停止：Fragment onStop position=" + this.getPosition());
    }

    /**
     * 销毁碎片视图
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.debug(TAG + " 销毁碎片视图：Fragment onDestroyView position=" + this.getPosition());
    }

    /**
     * 页面销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.debug(TAG + " 页面销毁：Fragment onDestroy position=" + this.getPosition());
    }

    /**
     * 把碎片从页面撕下来
     */
    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.debug(TAG + " 把碎片从页面撕下来：Fragment onDetach position=" + this.getPosition());
    }
}